package com.project.file.helper;

import com.project.bean.file.SaveFileOutDTO;
import com.project.file.factory.FileFactory;
import com.project.file.tool.BaseFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Objects;

/**
 * @author mengxj
 */
public class FileProxy extends BaseFile {

    private static BaseFile baseFile;

    @Override
    public boolean removeFile(String bucket, String objectName){
        this.checkAndInit();
        return baseFile.removeFile(bucket, objectName);
    }

    @Override
    public SaveFileOutDTO saveFile(String fileName, String bucket, InputStream is, String suffix) throws Exception {
        this.checkAndInit();
        return baseFile.saveFile(fileName, bucket, is, suffix);
    }

    @Override
    public void download(String bucket, String objectName, String fileName){
        this.checkAndInit();
        baseFile.download(bucket, objectName, fileName);
    }

    @Override
    public SaveFileOutDTO saveFile(MultipartFile file){
        this.checkAndInit();
        return baseFile.saveFile(file);
    }

    private void checkAndInit(){
        if(Objects.isNull(baseFile)){
            baseFile = FileFactory.getInstance();
        }
    }
}
